<template>
  <a-layout>

    <a-layout-content
        :style="{ background: '#fff', padding: '24px', margin: 0, minHeight: '280px' }"
    >

      <p>

        <a-form
            layout="inline"
            :model="formState"
            @finish="handleQuery({page:1,size:pagination.pageSize})"
        >
          <a-form-item>
            <a-input v-model:value="formState.name" placeholder="书籍名称">
              <template #prefix>
                <UserOutlined style="color: rgba(0, 0, 0, 0.25)"/>
              </template>
            </a-input>
          </a-form-item>

          <a-form-item>
            <a-button type="primary" @click="handleQuery({page:1,size:pagination.pageSize})">查询</a-button>
          </a-form-item>


          <a-forom-item>
            <a-button type="primary" @click="add()" >新增</a-button>
          </a-forom-item>

        </a-form>

      </p>


      <a-table
          :columns="columns"
          :row-key="record=>record.id"
          :data-source="level1"
          :pagination="pagination"
          :loading="loading"
          @change="handleTableChange"
      >
        <!--分类封面-->
        <template #cover="{text:cover}">
          <img style="height: 50px;width: 50px" v-if="cover" :src="cover" alt="avatar"/>
        </template>

        <!--按钮-->
        <template v-slot:action="{text,record}">
          <a-space size="small">
            <a-button type="primary" @click="edit(record)">编辑</a-button>
            <a-popconfirm
                title="确认删除？"
                ok-text="确认"
                cancel-text="取消"
                @confirm="del(record.id)"
                @cancel="cancel"

            >
              <a-button type="danger">删除</a-button>

            </a-popconfirm>
          </a-space>
        </template>
      </a-table>
    </a-layout-content>
  </a-layout>

  <!--编辑对话框-->
  <a-modal
      title="Title"
      v-model:visible="modalVisible"
      :confirm-loading="ModalLoading"
      @ok="handleModalOk"
  >

    <a-form :model="category" :label-col="{span:6}" :wrapper-col="{span:18}">

      <a-form-item label="名称">
        <a-input v-model:value="category.name"/>
      </a-form-item>
      <a-form-item label="父分类">

        <a-select
            ref="select"
            v-model:value="category.parent"
        >
          <a-select-option value="0">无</a-select-option>
          <a-select-option v-for="c in level1" :key="c.id" :value="c.id" :disabled="category.id===c.id">
            {{c.name}}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="顺序">
        <a-input v-model:value="category.sort"/>
      </a-form-item>


    </a-form>

  </a-modal>

</template>

<script lang="ts">
import {defineComponent, onMounted, ref} from 'vue';
import axios from "axios";
import {message} from "ant-design-vue";
import {UserOutlined, LockOutlined} from '@ant-design/icons-vue';
import {Tool} from "@/util/tool.ts";

export default defineComponent({
  name: 'AdminCategory',
  components: {
    UserOutlined,
    LockOutlined,
  },

  setup() {
    const level1=ref();
    const categorys = ref();
    const formState = ref({
      name: ''
    })
    const pagination = ref({
      hideOnSinglePage: true,
      current: 1,
      pageSize: 100,
      total: 0
    });
    const loading = ref(false);
    const columns = [

      {
        title: '名称',
        dataIndex: 'name'
      },
      {
        title: '父分类',
        key: 'parent',
        dataIndex: 'parent'
      },
      {
        title: '顺序',
        dataIndex: 'sort',
      },
      {
        title: 'Action',
        key: 'action',
        slots: {customRender: 'action'}
      }
    ];

    /*数据查询*/
    const handleQuery = (p: any) => {
      loading.value = true;
      level1.value=[];
      axios.get("/category/list", {
        params: {
          name: formState.value.name,
          page: p.page,
          size: p.size
        }
      }).then((response) => {
        loading.value = false;
        const data = response.data;
        if (data.success) {
          categorys.value = data.content.list;
          console.log("原始数据:",categorys.value);
          level1.value=[];
          level1.value=Tool.array2Tree(categorys.value,0);
          console.log("树形结构:",level1);
          //重置分页按钮
          pagination.value.current = p.page;
          pagination.value.total = data.content.total;
        } else {
          message.error(data.message);
        }

      })
    };
    /*表格点击页码时触发*/
    const handleTableChange = (pagination: any) => {
      console.log("自带分页参数:" + pagination.current);
      if (formState.value.name == '') {
        handleQuery({
          page: pagination.current,
          size: pagination.pageSize
        })
      } else {
        handleQuery({
          name: formState.value.name,
          page: pagination.current,
          size: pagination.pageSize
        })
      }
    };
    /*表单*/
    const category = ref({});
    const modalVisible = ref(false);
    const modalLoading = ref(false);
    const handleModalOk = () => {
      modalLoading.value = true;
      axios.post("/category/save", category.value).then((response) => {
        modalLoading.value = false;
        const data = response.data;//data=commonResp

        if (data.success) {
          modalVisible.value = false;
          //重新加载列表
          handleQuery({
            page: pagination.value.current,
            size: pagination.value.pageSize
          });
          message.success("操作成功");
        } else {
          message.error(data.message)
        }
      });
    }

    onMounted(() => {
      handleQuery({
        page: 1,
        size: pagination.value.pageSize
      });
    });

    const edit = (record: any) => {
      modalVisible.value = true;
      category.value = Tool.copy(record);
    }

    const add = (record: any) => {
      modalVisible.value = true;
      category.value = {};

    }

    const del = (id: number) => {
      axios.delete("/category/delete/" + id).then((response) => {
        const data = response.data;//data=commonResp
        if (data.success) {
          //重新加载列表
          handleQuery({
            page: pagination.value.current,
            size: pagination.value.pageSize
          });
        }
      });

    }
    /*搜索*/
    const handleFinish = (p: any) => {
      handleQuery({
        name: formState.value.name,
        page: 1,
        size: pagination.value.pageSize
      })
    }
    const handleFinishFailed = () => {
      console.log("error")
    }

    return {
     // categorys,
      level1,
      category,
      add,
      del,
      pagination,
      columns,
      loading,
      handleTableChange,
      edit,
      modalVisible,
      modalLoading,
      handleModalOk,
      formState,
      handleFinish,
      handleFinishFailed,
      handleQuery
    }
  }


});
</script>
